import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class Milkshakes {
	private static class Customer {
		int T;
		Map<Integer, Integer> XY = new HashMap<Integer, Integer>();
		public Customer(int T) {
			this.T = T;
		}
	}
	public static void main(String[] args) throws IOException {
		BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
		try {
			int C = Integer.parseInt(rdr.readLine());
			c: for(int c = 1; c <= C; c++) {
				int N = Integer.parseInt(rdr.readLine());
				int M = Integer.parseInt(rdr.readLine());
				List<Customer> customers = new ArrayList<Customer>();
				Queue<Customer> q = new LinkedList<Customer>();
				for(int m = 0; m < M; m++) {
					String[] tok = rdr.readLine().split(" ");
					Customer customer = new Customer(Integer.parseInt(tok[0]));
					for(int i = 0; i < customer.T; i++)
						customer.XY.put(Integer.parseInt(tok[i * 2 + 1]), Integer.parseInt(tok[i * 2 + 2]));
					if(customer.T == 1 && Integer.parseInt(tok[2]) == 1)
						q.add(customer); // selecting the customer's choice as he has got only one choice
					else
						customers.add(customer);
				}
				int malted[] = new int[N]; // 0 - unmalted, 1 - malted
				while(!q.isEmpty()) {
					Customer qcust = q.remove();
					int flavor = qcust.XY.keySet().iterator().next();
					if(malted[flavor - 1] < 1)
						malted[flavor - 1] = 1;
					else
						continue; // continuing as the flavor is already chosen the same
					for(int i = 0; i < customers.size(); i++) {
						Customer cust = customers.get(i);
						Integer icustmalted = cust.XY.get(flavor);
						if(icustmalted == null)
							continue;
						int custmalted = icustmalted.intValue();
						if(custmalted == 1)
							customers.remove(i--); // removing as the customer has got his choice
						else {
							cust.XY.remove(flavor);
							cust.T--;
							if(cust.T < 1) {
								// IMPOSSIBLE as the customer has no choices left
								System.out.println("Case #" + c + ": IMPOSSIBLE");
								continue c;
							} else if(cust.T == 1 && cust.XY.get(cust.XY.keySet().iterator().next()) == 1) {
								q.add(cust);
								customers.remove(i--);
							}
						}
					}
				}
				System.out.print("Case #" + c + ":");
				for(int i = 0; i < N; i++)
					System.out.print(" " + malted[i]);
				System.out.println();
			}
		} finally {
			rdr.close();
		}
	}
}
